package 二叉搜索树;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/
 */
public class _230_二叉搜索树中第K小的元素 {

    public int kthSmallest1(TreeNode root, int k) {
        Stack<TreeNode> stack = new Stack<>();
        int num = 0;
        int res = -1;
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            // 节点不为空一直压栈
            while (cur != null) {
                stack.push(cur);
                cur = cur.left; // 考虑左子树
            }
            // 节点为空，就出栈
            cur = stack.pop();
            // 当前值加入
            num++;
            if (num == k) {
                res = cur.val;
                break;
            }
            // 考虑右子树
            cur = cur.right;
        }
        return res;
    }

    int result;
    int i = 0;

    public int kthSmallest(TreeNode root, int k) {
        order(root, k);
        return result;
    }

    public void order(TreeNode root, int k) {
        if (root == null) return;
        order(root.left, k);
        i++;
        if (i == k) {
            result = root.val;
            return;
        }
        order(root.right, k);
    }
}
